/*
 * ROL.c
 *
 *  Created on: Jan 20, 2014
 *      Author: adijoshi
 */

#include "includes.h"

/*
 * Function: func_ROL
 *
 * Inputs:	 Mnemonic, Address_of_source, Address_of_destination
 * Outputs:  Result
 * Condition  Codes:
 * 			Z : set  if  all  bits  of  result  =  0;  cleared  otherwise
 * 			N : set  if  the  high-order  bit  of  the  result  is  set;  cleared otherwise
 * 			C : loaded  with  the  high-order  bit  of  the  destination
 * 			V : loaded  with  the  Exclusive  OR  of  the  N-bit  and  C-bit
 * 				(as  set  by  the  completion  of  the  rotate  operation).
 *
 Rotates  all  bits  of  the  destination  left  one  place.
 Bit  15  is  loaded into  the  C-bit  of  the  status  word  and
 the  previous  contents  of  the C-bit  are loaded  into  bit  0  of  the  destination.

 */

void func_ROL(unsigned short destination_address)
{
	unsigned short destination_data,result = 0;
	unsigned short destination_MSb,carry = 0;

	destination_data = read_from_memory(destination_address,DATA);
	destination_MSb = (destination_data & 0x8000);
	carry = get_C_flag();

	result = (destination_data << 1);

	if(carry)
		result = result | 0x0001;

	write_in_memory(destination_address,result,DATA);

	update_Z_flag(result);
	update_N_flag(ROL,result);

	if(destination_MSb)
		set_C_flag();
	else
		clear_C_flag();

	// update V flag only after updating C flag
	if(get_C_flag() ^ get_N_flag())
		set_V_flag();
	else
		clear_V_flag();
}

